/**
 * 初始化数据源定义详情对话框
 */
var SdsStandSourceInfoDlg = {
    validateFields: {
        sourceCode: {
            validators: {
                notEmpty: {
                    message: '数据源代码不能为空'
                }
            }
        },
        sourceDesc: {
            validators: {
                notEmpty: {
                    message: '数据源名称不能为空'
                }
            }
        },
        isActive: {
            validators: {
                notEmpty: {
                    message: '是否有效不能为空'
                }
            }
        },
    }
};


/**
 * 关闭此对话框
 */
SdsStandSourceInfoDlg.close = function() {
    parent.layer.close(parent.layer.getFrameIndex(window.name)); //先得到当前iframe层的索引,再执行关闭
}

/**
 * 验证数据是否为空
 */
SdsStandSourceInfoDlg.validate = function () {
    $('#sdsStandSourceInfoForm').data("bootstrapValidator").resetForm();
    $('#sdsStandSourceInfoForm').bootstrapValidator('validate');
    return $("#sdsStandSourceInfoForm").data('bootstrapValidator').isValid();
}

/**
 * 提交新增
 */
SdsStandSourceInfoDlg.addSubmit = function() {

    if (!this.validate()) {
        return;
    }
    //提交信息
    // Sys.ajaxForm('sdsStandSourceInfoForm',Sys.backendPath + "/sds/sdsstanddatasource/to_add",function(res) {
    //     Sys.success(res.message);
    //     window.parent.SdsDataSource.table.refresh();
    //     SdsStandSourceInfoDlg.close();
    // });

    var queryData = {};
    var ajax = new $ax(Sys.backendPath + "/sds/sdsstanddatasource/to_add", function (data) {
        Sys.success(data.message);
        window.parent.SdsDataSource.table.refresh();
        SdsStandSourceInfoDlg.close();
    }, function (data) {
        Sys.error("修改失败!" + data.responseJSON.message + "!");
    });
    //ajax.set(this.qcRuleData);
    queryData['id'] = $("#id").val();
    queryData['sourceDesc'] = $("#sourceDesc").val();
    queryData['sourceCode'] = $("#sourceCode").val();
    queryData['parentId'] = $('#parentId').combotree('getValue');
    queryData['resume'] =  $("#resume").val();
    if ($("#isActive").prop('checked')){
        queryData['isActive'] = 1;
    }
    else {
        queryData['isActive'] = 0;
    }
    ajax.setData(queryData);
    ajax.start();
}

/**
 * 提交修改
 */
SdsStandSourceInfoDlg.editSubmit = function() {

    if (!this.validate()) {
        return;
    }
    //提交信息
    // Sys.ajaxForm('sdsStandSourceInfoForm',Sys.backendPath + "/sds/sdsstanddatasource/update",function(res) {
    //     Sys.success(res.message);
    //     window.parent.SdsStandSource.table.refresh();
    //     SdsStandSourceInfoDlg.close();
    // });

    var queryData = {};
    var ajax = new $ax(Sys.backendPath + "/sds/sdsstanddatasource/update", function (data) {
        Sys.success(data.message);
        window.parent.SdsDataSource.table.refresh();
        SdsStandSourceInfoDlg.close();
    }, function (data) {
        Sys.error("修改失败!" + data.responseJSON.message + "!");
    });
    //ajax.set(this.qcRuleData);
    queryData['id'] = $("#id").val();
    queryData['sourceDesc'] = $("#sourceDesc").val();
    queryData['sourceCode'] = $("#sourceCode").val();
    queryData['parentId'] = $('#parentId').combotree('getValue');
    queryData['resume'] =  $("#resume").val();
    queryData['isActive'] = $("#isActive").checkbox("getValue") ? 1 : 0;
    ajax.setData(queryData);
    ajax.start();
}

$(function() {
    $HUI.combotree('#parentId',{
        url: '/sds/sdsstanddatasource/getDataSourceTree'
    });
    Sys.initValidator("sdsStandSourceInfoForm", SdsStandSourceInfoDlg.validateFields);
});
